package cn.sc.summer.oss.service.impl;

import cn.sc.summer.oss.service.MinioService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 类名：minio初始化
 *
 * @author a-xin
 * @date 2024/1/29 09:21
 */
@Slf4j
@Service
public class MinioServiceImpl implements MinioService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传文件
     *
     * @param uploadFile 文件流
     * @return 文件地址
     */
    @Override
    public String uploadFile(MultipartFile uploadFile) {
        FileInfo upload = fileStorageService.of(uploadFile).upload();
        return upload.getPath();
    }

    /**
     * 下载文件
     *
     * @param fileId   文件地址
     * @param response 返回数据信息
     */
    @Override
    public void downloadFile(String fileId, HttpServletResponse response) {
        try {
            fileStorageService.download(fileId).outputStream(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
